package LeetCode.top100;

import java.util.ArrayList;
import java.util.List;

public class LC438 {
    public List<Integer> findAnagrams(String s, String p) {
        int[] arr1 = new int[26];
        int[] arr2 = new int[26];
        for(int i = 0; i < p.length(); i++){
            arr2[p.charAt(i)-'a']++;
        }
        List<Integer> list = new ArrayList<>();
        for(int left = 0, right = 0; right < s.length(); right++){
            arr1[s.charAt(right)-'a']++;
            if(right - left + 1 == p.length()){
                if(isResult(arr1, arr2)){
                    list.add(left);
                }
                arr1[s.charAt(left)-'a']--;
                left++;
            }
        }
        return list;
    }
    public boolean isResult(int[] arr1, int[] arr2){
        for(int i = 0; i < 26; i++){
            if(arr1[i] != arr2[i]){
                return false;
            }
        }
        return true;
    }
}
